﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Hanhud.Win
{
    public class DGVManagement
    {
        public event EventHandler BindedData;

        void OnBindedData(EventArgs e)
        {
            if (BindedData != null)
            {
                BindedData(this, e);
            }        
        }

        public DataGridView DataGridView
        {
            get;
            set;
        }

        public DataGridViewColumn[] Columns
        {
            get;
            set;
        }

        public DataTable DataTable
        {
            get;
            set;
        }

        public void FormatColumns()
        {
            if (Columns == null) throw new Exception("Columns is null.");

            if (Columns.Length >= DataGridView.Columns.Count)
            {
                for (int i = 0; i < DataGridView.Columns.Count; i++)
                {
                    DataGridView.Columns[i].Name = Columns[i].Name;
                    DataGridView.Columns[i].HeaderText = Columns[i].HeaderText;
                    DataGridView.Columns[i].Width = Columns[i].Width;
                    DataGridView.Columns[i].DefaultCellStyle = Columns[i].DefaultCellStyle;                    
                }
            
            }
        
        }

        public void CopyColumns()
        {
            if (DataGridView == null) throw new Exception("DataGridView is null.");

            List<DataGridViewColumn> list = new List<DataGridViewColumn>();
            foreach (DataGridViewColumn col in DataGridView.Columns)
            {
                if (col.Visible)
                    list.Add(col);
            }

            Columns = list.ToArray();   
        }

        public void CopyColumns(DataGridView grid)
        {
            if (grid == null) throw new Exception("DataGridView is null.");

            List<DataGridViewColumn> list = new List<DataGridViewColumn>();
            foreach (DataGridViewColumn col in grid.Columns)
            {
                if (col.Visible)
                    list.Add(col);
            }

            Columns = list.ToArray();
        }

        public void BindData()
        {
            if (DataGridView == null) throw new Exception("DataDataGrid is null.");
            if (DataTable == null) throw new Exception("DataTable is null.");

            DataTable dt = DataTable.Copy();

            DataTable oldTable = (DataTable)DataGridView.DataSource;
            DataGridView.DataSource = dt;

            if (oldTable != null) oldTable.Dispose();

            FormatColumns();
            OnBindedData(EventArgs.Empty);
        }

        public void UnbindData()
        {
            DataGridView.DataSource = null;
        
        }

        public void AddRow(params object[] values)
        {
            if (DataGridView.DataSource == null) throw new Exception("DataGridView's DataSource is null");

            (DataGridView.DataSource as DataTable).Rows.Add(values);
        
        }



    }
}
